import { message } from "antd"
import api from "./request"

/**
 * AI搜索
 * @param params 
 * @returns 
 */
export const fetchAISearch = async (params: {
    userGuid: string,
    labelWord?: string,
    keyWord?: string
}): Promise<any> => {
    try {
        const response = await api.post('/Info/SearchInfoAI', {
            userGuid: params.userGuid,
            labelWord: params.labelWord ?? '',
            keyWord: params.keyWord ?? ''
        })

        return response
    } catch (error) {
        message.error('搜索知识库信息失败')

        return Promise.reject(error)
    }
}

/**
 * 获取知识库信息
 * @param params 
 * @returns 
 */
export const fetchAllInfo = async (params: {
    userGuid: string,
    labelWord: string,
    keyWord?: string,
    pageNumber: number,
    pageSize: number
}): Promise<any> => {
    try {
        const response = await api.post('/Info/GetAllInfo', {
            userGuid: params.userGuid,
            labelWord: params.labelWord,
            keyWord: params.keyWord ?? '',
            pageNumber: params.pageNumber,
            pageSize: params.pageSize
        })

        return response.data
    } catch (error) {
        message.error('获取知识库信息失败')

        return Promise.reject(error)
    }
}

/**
 * 获取文章详情
 * @param params 
 * @returns 
 */
export const fetchArticleDetail = async (params: {
    infoGuid: string
    userGuid: string
}) => {
    try {
        const response = await api.post(`/Info/GetInfoId`,{
            infoGuid:params.infoGuid
        })

        return response.data
    } catch (error) {
        message.error('获取文章详情失败')

        return Promise.reject(error)
    }
}

/**
 * 获取标签信息
 * @param params 
 * @returns 
 */
export const fetchLabels = async (params: {
    userGuid: string
}): Promise<string[]> => {
    try {
        const response = await api.post('/Info/GetLabels', params)

        return response.data
    } catch (error) {
        message.error('获取标签信息失败')

        return Promise.reject(error)
    }
}

/**
 * 更新阅读数
 * @param params 
 * @returns 
 */
export const fetchAddReadCount = async (params: {
    userGuid: string,
    infoGuids: string[]
}): Promise<any> => {
    try {
        const response = await api.post('/Info/SetReadCount', {
            infoGuids: params.infoGuids,
        })

        return response.data
    } catch (error) {
        message.error('更新阅读数失败')

        return Promise.reject(error)
    }
}

/**
 * 设置公开
 * @param params 
 * @returns 
 */
export const fetchSetArticlePublic = async (params: {
    infoGuid: string,
    isPublic: 0 | 1
}) => {
    try {
        const response = await api.post('/Info/SetPublic',params)

        return response.data
    } catch (error) {
        message.error('设置失败')

        return Promise.reject(error)
    }
}

/**
 * 复制
 * @param params 
 * @returns 
 */
export const fetchCopyArticle = async (params: {
    infoGuid: string,
    userGuid: string
}) => {
    try {
        const response = await api.post('/Info/CopyInfo',params)

        return response.data
    } catch (error) {
        message.error('复制失败')

        return Promise.reject(error)
    }
}

/**
 * 删除文章
 * @param params 
 * @returns 
 */
export const fetchDeleteArticle = async (params: {
    infoGuids: string[]
}) => {
    try {
        const response = await api.post('/Info/DelInfos',params)

        return response.data
    } catch (error) {
        message.error('删除失败')

        return Promise.reject(error)
    }
}